package biz

import (
	"log"
	"net/http"
)

type Error struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
}

func NewError(code int, msg string) *Error {
	return &Error{
		Code: code,
		Msg:  msg,
	}
}

func (e *Error) Error() string {
	return e.Msg
}

func ErrHandler(err error) (int, any) {
	log.Println("err:", err)
	switch e := err.(type) {
	case *Error:
		// 自定义一个 错误返回类型
		return http.StatusOK, Fail(e)
	default:
		return http.StatusInternalServerError, UnknownFail(err.Error())
	}
}
